//
//  UATVersionControl.m
//  UStarter
//
//  Created by Mark on 2021/1/9.
//

#import "UATVersionControl.h"
#import "UATStarter.h"
#import "UATApi.h"
@implementation UATVersionControl

+ (void) getLastestVersionBlock:(NewVersionBlock)block {
    NSString * appId = [[[UATStarter shareInstance] launchConfig] appId];
    if(appId==nil){
        return ;
    }
    [UATApi doGetAction:@"https://itunes.apple.com/cn/lookup" withParam:@{@"id":appId} callback:^(UATApiRequstStatus status, id  _Nullable responseObjec, NSProgress * _Nullable progress, NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        NSError * err ;
        if(status==UATApiRequestStatusSUCCESS){
            
            UATVersionEntity * version = [[UATVersionEntity alloc] initWithDictionary:responseObjec error:&err];
            if(version.resultCount>=1){
                UATVersionEntityResult * res = [version results][0] ;
                NSString * remoteVersionStr = res.version ;
                BOOL isNew = [UatUtil compareRemoteAppVersion:remoteVersionStr];
                if(isNew){
                    DLog(@"发现新版本:%@",remoteVersionStr);
                    block(version);
                }else{
                    DLog(@"没发现新版本");
                }
            }else{
                DLog(@"获取数据错误");
            }
            
        }
        if(status == UATApiRequestStatusFAIL){
            DLog(@"请求数据失败:%@",error);
        }
    }];
    
}

@end
